import RequestProcess from "./RequestProcess";
import operator from "../operator/Operator";
import OperateArgs from "../operator/OperateArgs";
import OperatorDecorator from "../operator/OperatorDecorator";
import JwtGenerateOperator from "../operator/jwtGenerateOperator";
import { RC } from "../config/RC";
import CheckPasswordOperator from "../operator/CheckPasswordOperator";
import CheckIdExistOperator from "../operator/CheckUserIdExistOperator";
import GetUserNameByIdOperation from "../operator/GetUserNameByIdOperation";

class LogInRequestProcess extends RequestProcess {

    public args_generate(): void {
        this.args = new OperateArgs();
        this.args.setProperty("userId", this.req.body.userId);
        this.args.setProperty("password", this.req.body.password);
    }

    public operator_plan_generate(): void {
        const check_id_exist_operator = new CheckIdExistOperator(this.args);
        const check_password_correct = new CheckPasswordOperator(this.args);
        const jwt_generate_operator = new JwtGenerateOperator(this.args);
        const get_user_name_operation = new GetUserNameByIdOperation(this.args);

        this.operator_plan = new OperatorDecorator(check_id_exist_operator, check_password_correct);
        this.operator_plan = new OperatorDecorator(jwt_generate_operator, this.operator_plan);
        this.operator_plan = new OperatorDecorator(get_user_name_operation,this.operator_plan);
    }

    public writeResult(rc: RC): Object {
        switch (rc) {
            case RC.SUCCESS:
                return {
                    code: "0000",
                    msg: "登录成功",
                    userName: this.getOperateArgs().getProperty("userName"),
                    token: this.getOperateArgs().getProperty("token")
                }
            case RC.USERID_NOTEXIST:
                return {
                    code: "1004",
                    msg: "ID不存在"
                }
            case RC.INVALID_ARGUMENT:
                return {
                    code: "1002",
                    msg: "参数不合法"
                }
            case RC.INTERNAL_ERROR:
                return {
                    code: "1003",
                    msg: "服务器内部错误"
                }
            case RC.PASSWORD_INCORRECT:
                return {
                    code: "1005",
                    msg: "密码错误"
                }

            default:
                return {
                    code: "2000",
                    msg: "未知错误"
                }
        }
    }
}

export default LogInRequestProcess;